package com.aierkeji.b2c.dao.jpa;

import java.math.BigDecimal;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.aierkeji.b2c.dao.BaseJPADao;
import com.aierkeji.b2c.entity.AkjOrderRecord;
import com.aierkeji.b2c.entity.constant.OrderStatus;
import com.aierkeji.b2c.entity.vo.OrderDetailVo;
import com.aierkeji.b2c.entity.vo.OrderListVo;

public interface AkjOrderRecordDao extends BaseJPADao<AkjOrderRecord, String>{

	
	@Query("select t from OrderListVo t where t.isDelete = 0 and t.aliPayUserId = :aliPayUserId and t.lifeId = :lifeId")
	public Page<OrderListVo> getOrderList(@Param("aliPayUserId") String aliPayUserId, @Param("lifeId") String lifeId,Pageable pageable);
	
	AkjOrderRecord findByIdAndAliPayUserIdAndLifeIdAndIsDeleteAndOrderStatus(String id,String AliPayUserId,String LifeId ,Boolean isDelete,OrderStatus orderStatus);

	
	@Modifying
	@Query("update AkjOrderRecord set orderStatus=:newOrderStatus , updateTime = :now,remark=:remark,isRefund= '1',refundTime=:now,refundPremium=:refundPremium where "
			+ "id = :#{#akjOrderRecord.id} "
			+ "and orderStatus=:#{#akjOrderRecord.orderStatus} "
			+ "and aliPayUserId=:#{#akjOrderRecord.aliPayUserId} "
			+ "and lifeId=:#{#akjOrderRecord.lifeId} and isDelete= 0")
	int updateCancle(AkjOrderRecord akjOrderRecord,@Param("newOrderStatus") OrderStatus newOrderStatus,@Param("now") java.sql.Timestamp now,@Param("remark") String remark,@Param("refundPremium") BigDecimal refundPremium) ;
	
	
	@Query("select new com.aierkeji.b2c.entity.vo.OrderDetailVo(t.orderNo,t.client,t.location,t.phone,t.productName,t.orderStatus,t.borrowTime,a.tel,t.deliveryCompany,t.deliveryOrder"
			+ ",t.id,t.aliPayUserId,t.lifeId) from AkjOrderRecord t join AkjAlipayLifeSnap a  on t.lifeId = a.id where t.isDelete = 0 and t.orderNo = :orderNo ")
	public Page<OrderDetailVo> getOrderList(@Param("orderNo") String orderNo,Pageable pageable);
	
	
}
